JavaScript Relational Operators

Visualizing `<`, `>`, `<=`, `>=`, `instanceof`, and `in`.

Comparison (`<`, `>`, `<=`, `>=`)

These operators compare two operands and return a boolean value (`true` or `false`). They perform type coercion, which can lead to unexpected results when comparing different types.

5 > 3;    // true
10 <= 10; // true
"2" > 1;  // true (string "2" is coerced to number 2)

Numeric Comparison

Expression: `10 > 5`

Result:

Expression: `10 <= 5`

Result:

Expression: `10 <= 10`

Result:

Mixed-Type Comparison

Expression: `"10" > 5`

Result:

Expression: `10 < "5"`

Result:

Expression: `"a" > "b"`

Result:


`instanceof` and `in`

**`instanceof`** checks if an object is an instance of a particular class or constructor function. **`in`** checks if a property exists in a specified object or its prototype chain.

const d = new Date();
d instanceof Date; // true
d instanceof Object; // true

const user = { name: "Alice" };
"name" in user; // true
"age" in user; // false

`instanceof`

Object: const d = new Date()

Expression: `d instanceof Date`

Result:

Expression: `d instanceof Object`

Result:

Expression: `d instanceof Array`

Result:

`in`

Object: const person = { name: 'Bob' }

Expression: `"name" in person`

Result:

Expression: `"toString" in person`

Result:

Expression: `"age" in person`

Result: